package com.hyphenate.easeui.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.util.Util;
import com.hyphenate.easeui.R;
import com.hyphenate.util.Utils;

import java.util.HashSet;
import java.util.Set;

/**
 * time:2018/3/6
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class JGPingJia extends FrameLayout {
    private int arg1 = 0, arg2, arg3;
    private String comment;
    private int selectType;
    private RadioButton many, sort, less;
    private TextView mDisPlayPJtv, mCommitPJtv, mTitle, jg_tv_speed,tvHfPJ;
    private EditText mInputPGet;
    private RadioGroup radioGroup;
    private Set<String> typeSet1, typeSet2, typeSet3;
    //分割线，当展示答案的时候需要显示，输入答案时需要隐藏
    private FrameLayout mLines;
    private RelativeLayout mInputRL;
    private onCommit mOnCommit;
    private LabelLayout payPurposes1, payPurposes2, payPurposes3;
    private ScrollView svHfPj,scroll_view;
    private LinearLayout llDisplay;
    public JGPingJia(@NonNull Context context) {
        super(context);
        initView(context);

    }


    public JGPingJia(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public JGPingJia(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);

    }

    private EmojiFilter inputFilter;
    InputFilter[] inputFilters;

    private void initView(final Context context) {
        final View view = LayoutInflater.from(context).inflate(R.layout.jg_pingjia_view, this, false);
        inputFilter = new EmojiFilter();
        inputFilters = new InputFilter[]{inputFilter};
        mDisPlayPJtv = (TextView) view.findViewById(R.id.tv_display_pj);
        mCommitPJtv = (TextView) view.findViewById(R.id.tv_commit_pj);
        mInputPGet = (EditText) view.findViewById(R.id.et_pj_content);
        etHfPj = (EditText) view.findViewById(R.id.etHfPj);
        mInputPGet.setFilters(inputFilters);
        etHfPj.setFilters(inputFilters);
        mLines = (FrameLayout) view.findViewById(R.id.fl_dispaly_line);
        llDisplay = (LinearLayout) view.findViewById(R.id.llDisplay);
        mTitle = (TextView) view.findViewById(R.id.jg_tv_title);
        jg_tv_speed = (TextView) view.findViewById(R.id.jg_tv_speed);
        tvHfPJ = (TextView) view.findViewById(R.id.tvHFPJ);
        mInputRL = (RelativeLayout) view.findViewById(R.id.rl_input_pj);
        radioGroup = (RadioGroup) view.findViewById(R.id.rgSatisfied);
        many = (RadioButton) view.findViewById(R.id.many);
        sort = (RadioButton) view.findViewById(R.id.sort);
        less = (RadioButton) view.findViewById(R.id.less);
        payPurposes1 = (LabelLayout) view.findViewById(R.id.payPurposes1);
        payPurposes2 = (LabelLayout) view.findViewById(R.id.payPurposes2);
        payPurposes3 = (LabelLayout) view.findViewById(R.id.payPurposes3);
        svHfPj = (ScrollView) view.findViewById(R.id.svHfPj);
        scroll_view = (ScrollView) view.findViewById(R.id.scroll_view);
        typeSet1 = new HashSet<>();
        typeSet2 = new HashSet<>();
        typeSet3 = new HashSet<>();
        setLabelText();
        selectType = 5;
        arg1 = 5;
        arg2 = 5;
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.many) {
                    selectType = 5;
                    arg1 = 5;
                    arg2 = 5;
                    payPurposes2.setVisibility(GONE);
                    payPurposes3.setVisibility(GONE);
                    payPurposes1.setVisibility(VISIBLE);
                    jg_tv_speed.setText("请给工程师一些评价吧");
                } else if (checkedId == R.id.sort) {
                    selectType = 4;
                    arg1 = 4;
                    arg2 = 4;
                    payPurposes2.setVisibility(VISIBLE);
                    payPurposes3.setVisibility(GONE);
                    payPurposes1.setVisibility(GONE);
                    jg_tv_speed.setText("请给工程师一些评价吧");
                } else {
                    selectType = 3;
                    payPurposes2.setVisibility(GONE);
                    payPurposes3.setVisibility(VISIBLE);
                    payPurposes1.setVisibility(GONE);
                    jg_tv_speed.setText("请给工程师一些建议或者告诉我们您的不满意之处");
                    arg1 = 3;
                    arg2 = 3;
                }
            }
        });
        mCommitPJtv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(commitHF){
                    if(TextUtils.isEmpty(etHfPj.getText().toString())){
                        Toast.makeText(context, "请填写回复内容", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mOnCommit.onCommit(-1, -1, -1, etHfPj.getText().toString());
                    return;
                }
                if (arg1 == 0) {
                    Toast.makeText(context, "请选择满意度", Toast.LENGTH_SHORT).show();
                    return;
                }

                String typset = "";
                if (selectType == 5) {
                    for (String s : typeSet1) {
                        typset += s + " ";
                    }
                } else if (selectType == 4) {
                    for (String s : typeSet2) {
                        typset += s + " ";
                    }
                } else {
                    for (String s : typeSet3) {
                        typset += s + " ";
                    }
                }

                if (null != mOnCommit) {
                    mOnCommit.onCommit(arg1, arg2, arg2, typset + "\n" + mInputPGet.getText().toString());
//                            setPJUser(star1.getStar(), star2.getStar(), star3.getStar(), mInputPGet.getText().toString());
                }
            }
        });

//        mInputPGet.setOnKeyListener(new OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
//                if (keyCode == KeyEvent.KEYCODE_DEL) {
//                    //this is for backspace
//                    Log.e("222222222222", "333333333");
//                    if (mInputPGet.getText().length() > 0) {
//                        String inputString = mInputPGet.getText().toString();
//                        inputString = inputString.substring(0, inputString.length() - 1);
//                        mInputPGet.setText(inputString);
//                        CharSequence text = mInputPGet.getText();
//                        if (text instanceof Spannable) {
//                            Spannable spanText = (Spannable) text;
//                            Selection.setSelection(spanText, text.length());
//                        }
//                    }
//                }
//                return true;
//            }
//        });

        addView(view);
    }

    public void disableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(false);
        }
    }

    private void setLabelText() {
        String status1 = "响应速度快,解答专业度高,态度非常好,指导细心,故障得到解决";
        String status2 = "响应及时,故障分析准确,态度良好,专业度较好";
        String status3 = "响应速度稍慢,解答内容简短,服务态度一般,专业度一般";
        String[] arr = status1.split(",");
        for (int i = 0; i < arr.length; i++) {
            View labelView = View.inflate(getContext(), R.layout.item_pay_purpose, null);
            final TextView textView = (TextView) labelView.findViewById(R.id.item);
            final TextView index = (TextView) labelView.findViewById(R.id.type);
            index.setText(String.valueOf(i));
            textView.setText(arr[i]);

            MarginLayoutParams lp = new MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            lp.topMargin = 5;
            lp.bottomMargin = 5;

            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (textView.isSelected()) {
                        textView.setSelected(false);
                        typeSet1.remove(textView.getText().toString());
                    } else {
                        textView.setSelected(true);
                        typeSet1.add(textView.getText().toString());
                    }
                }
            });

            payPurposes1.addView(labelView, lp);
        }
        String[] arr2 = status2.split(",");
        for (int i = 0; i < arr2.length; i++) {
            View labelView = View.inflate(getContext(), R.layout.item_pay_purpose, null);
            final TextView textView = (TextView) labelView.findViewById(R.id.item);
            final TextView index = (TextView) labelView.findViewById(R.id.type);
            index.setText(String.valueOf(i));
            textView.setText(arr2[i]);

            MarginLayoutParams lp = new MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            lp.topMargin = 5;
            lp.bottomMargin = 5;

            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (textView.isSelected()) {
                        textView.setSelected(false);
                        typeSet2.remove(textView.getText().toString());
                    } else {
                        textView.setSelected(true);
                        typeSet2.add(textView.getText().toString());
                    }
                }
            });

            payPurposes2.addView(labelView, lp);
        }
        String[] arr3 = status3.split(",");
        for (int i = 0; i < arr3.length; i++) {
            View labelView = View.inflate(getContext(), R.layout.item_pay_purpose, null);
            final TextView textView = (TextView) labelView.findViewById(R.id.item);
            final TextView index = (TextView) labelView.findViewById(R.id.type);
            index.setText(String.valueOf(i));
            textView.setText(arr3[i]);

            MarginLayoutParams lp = new MarginLayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            lp.topMargin = 5;
            lp.bottomMargin = 5;

            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (textView.isSelected()) {
                        textView.setSelected(false);
                        typeSet3.remove(textView.getText().toString());
                    } else {
                        textView.setSelected(true);
                        typeSet3.add(textView.getText().toString());
                    }
                }
            });

            payPurposes3.addView(labelView, lp);
        }
    }

    private boolean commitHF=false;
    public void setPJEngineer(String evaluateAnswerContent, int j, int k, String content) {
        disableRadioGroup(radioGroup);
        Log.e("evaluateAnswerContent", evaluateAnswerContent + "    ,,j=" + j + "   k=" + k);
        if (j==5) {
            many.setChecked(true);

        } else if (j==4) {
            sort.setChecked(true);
        } else {
            less.setChecked(true);
        }
        llDisplay.setVisibility(VISIBLE);
        payPurposes2.setVisibility(GONE);
        payPurposes3.setVisibility(GONE);
        payPurposes1.setVisibility(GONE);
        jg_tv_speed.setText("用户评价");
        mDisPlayPJtv.setText(content);
        mTitle.setText("用户已经对服务进行了评价");
        mLines.setVisibility(VISIBLE);
//
        if(TextUtils.isEmpty(evaluateAnswerContent)){//工程师未回复
            scroll_view.setVisibility(GONE);
            svHfPj.setVisibility(VISIBLE);
            tvHfPJ.setVisibility(GONE);
            commitHF = true;
        }else {
            mInputRL.setVisibility(GONE);
            tvHfPJ.setText(Html.fromHtml("<font color=\"#2196F3\">工程师回复：</font>"+evaluateAnswerContent));
        }


    }


    /**
     * 工程师回复后隐藏提交按键
     */
    public void hideSubmission() {
        etHfPj.setEnabled(false);
//        etHfPj.setFocusableInTouchMode(false);
//        etHfPj.setFocusable(false);
        mCommitPJtv.setVisibility(INVISIBLE);
    }

    /**显示工程师回复
     */
    private EditText etHfPj;

    public void setPJUser(String evaluateAnswerContent, int j, int k, String content) {
        disableRadioGroup(radioGroup);
        Log.e("satisfaction", evaluateAnswerContent + "    ,,j=" + j + "   k=" + k);
        if (j==5) {
            many.setChecked(true);
        } else if (j==4) {
            sort.setChecked(true);
        } else {
            less.setChecked(true);
        }
        mTitle.setText("您已对工程师的服务进行了评价");
        jg_tv_speed.setText("您的评价");
        payPurposes2.setVisibility(GONE);
        payPurposes3.setVisibility(GONE);
        payPurposes1.setVisibility(GONE);
        if(TextUtils.isEmpty(content))
            content = "用户没有输入任何内容";
        mDisPlayPJtv.setText(content);
        llDisplay.setVisibility(VISIBLE);
        mLines.setVisibility(VISIBLE);
        mInputRL.setVisibility(GONE);
        if(TextUtils.isEmpty(evaluateAnswerContent))
            evaluateAnswerContent = "暂无回复";
        tvHfPJ.setText(Html.fromHtml("<font color=\"#2196F3\">工程师回复：</font>"+evaluateAnswerContent));
    }

    public interface onCommit {
        void onCommit(int i, int j, int k, String content);
    }

    public void setOnCommitListener(onCommit onCommit) {
        this.mOnCommit = onCommit;
    }

}
